<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URLEncoder" %>
<title>EasySchedule</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="userValidate.js"></script>
<script type="text/javascript">
	function changeAdminKey(){
		$('#adminlabel').toggleClass('adminkey');
	}
	function goToAcnhor() { 
		window.location.hash="errorAnchor"; 
	}
</script>
</head>
	<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
	<%
	String uname,pass,fname,lname,email,phone;
	uname = pass = fname = lname = email = phone = "";
	String error="";
	if (request.getMethod().equals("POST"))
	{
		MultipartRequest req = new MultipartRequest(request,getServletContext().getRealPath("/")+File.separator+"public"+File.separator+"images");
		Map<String, String[]> params = new HashMap<String,String[]>();
		params.put("action", new String[]{"createNewUser"});
		Enumeration<String> map = req.getParameterNames();
		while(map.hasMoreElements())
		{
			String par = (String) map.nextElement();
			params.put(par, req.getParameterValues(par));
		}
		Result res = requestHandler.handle(params);
		if (res.getResultType().equals(ResultType.SUCCESS))
				{
					String redirect = request.getContextPath() + "/";
					request.getSession().setAttribute("username", req.getParameter("USER_NAME"));
					request.getSession().setAttribute("password", req.getParameter("PASSWORD"));
					response.sendRedirect(redirect);
				}
		else{
			Map<String,String[]> m = (Map<String,String[]>)res.getFailureReturnValue();
			error = m.get("error")[0];
			uname = m.get("USER_NAME")[0];
			pass = m.get("PASSWORD")[0];
			email = m.get("EMAIL")[0];
			fname = m.get("FIRST_NAME")[0];
			lname = m.get("LAST_NAME")[0];
			phone = m.get("PHONE")[0];
		}
	}
	String anchor = error.isEmpty()?"": "onLoad=\"goToAcnhor()\"";
	%>
<body <%=anchor %>>
    <div id="page">
	        <div id="header">
	            <h1><a href="/tamirandevyatar">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="/tamirandevyatar">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	            </ul>
	        </div>
        <div id="main">	
        	<div id="sidebar">
                <div class="box" id="login">
                    <h2 >Menu</h2>
                    <div class="box2">
                        <p>Please register so you could start using EasySchedule!</p>
                    </div>
                </div><!-- box -->				
            </div><!-- sidebar -->    	              
            <div id="content">
                <div id="contheader">
                    <h2>Registration</h2>
                </div>		
                <div id="content2" class="welcome">	
					<div class="entry">
						<form id = "reg" action="" method="post" enctype="multipart/form-data" onsubmit="return validateForm('create')">
							<label id="generalError" class="error"><%=error%></label>
							<p><a id="errorAnchor"></a>Please fill out the information below to sign up: (* marks required fields):</p>
							<table border="0">
								<tr>
								<td>
						    	<label for="USER_NAME">* Username &nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="USER_NAME" value="<%=uname%>" id="USER_NAME" class="aligned"/>
						    	<label id="USERERROR" class="error"></label>
						    	</td>
						    	</tr>
						    	<tr>
						    	<td>
						    	<label for="PASSWORD">* Password &nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="password" name="PASSWORD" value="" id="PASSWORD" class="aligned"/>
		              	        <label id="PASSERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="passwordConfirmation">* Password Confirmation&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="password" name="passwordConfirmation" value="" id="passwordConfirmation" class="aligned"/>
								<label id="PASSCONFERROR" class="error"></label>
		                        </td>
		                        </tr>
								<tr>
								<td>
								<label for="FIRST_NAME">First Name&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="FIRST_NAME" value="<%=fname%>" id="FIRST_NAME"/>
						    	<label id="FIRSTERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="LAST_NAME">Last Name&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="LAST_NAME" value="<%=lname%>" id="LAST_NAME"/>
						    	<label id="LASTERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="EMAIL">Email&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="EMAIL" value="<%=email%>" id="EMAIL"/>
						    	<label id="EMAILERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="PHONE">Phone number&nbsp;&nbsp;</label>
						    	</td>
						    	<td>
						    	<input type="text" name="PHONE" value="<%=phone%>" id="PHONE"/>
						    	<label id="PHONEERROR" class="error"></label>
								</td>
								</tr>
								<tr>
								<td>
								<label for="ROLE">Choose your role&nbsp;&nbsp;</label>
								</td>
								<td>
								<input type="radio" name="ROLE" id="ROLE" value="student" checked="checked" onchange="changeAdminKey()" /> student
								<input type="radio" name="ROLE" value="admin" id="admin" onchange="changeAdminKey();"/> admin
								</td>
								</tr>
								<tr id="adminlabel" class="adminkey">
								<td>
								<label for="adminkey">Enter admin key&nbsp;&nbsp;</label>
								</td>
								<td>
								<input type="password" name="adminkey" value="" id="adminkey"/>
								</td>
								</tr>
							</table>
						    <h2>
						    	<input type="submit" value="Register" />
						    	<input type="reset" value="Reset" />
					    	</h2>
				  		</form>
					</div>
                </div><!-- content2 -->								              
            </div><!-- content -->                    
            <div class="clearing">&nbsp;</div> 
            <div id="footer">
                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
            </div>
        </div><!-- main -->
    </div><!-- page -->
	</body>
</html>